<?php

namespace App\Http\Controllers;

use App\AdminModel;
use Illuminate\Http\Request;

class AdminController extends Controller
{

    //加载添加数据页面（form页面）;
    public function form()
    {
        return view("form");
    }



//添加数据
    public function insert(Request $request)
    {
        $data=$request->input();
        unset($data['_token']);
//        dd($data);
        $data['hobby']=implode("#",$data['hobby']);
        $ok=AdminModel::create($data);
        if($ok){
            return "数据添加成功<br>
            <a href='form'>数据添加</a><br>
            <a href='table'>查看数据</a>
            ";
        }else{
            return "数据添加失败<br>
            <a href='form'>数据添加</a><br>
            <a href='table'>查看数据</a>";
        }
    }

    public function update(Request $request)
    {
        $id=$request->input('id');
        $ok=AdminModel::where('id',$id)->first();
        return view("form",compact('ok'));
}


//查看数据
public function table()
{
    $data = AdminModel::get();
    return view('table', compact('data'));
}


//删除数据

    public function delete(Request $request)
    {
     $id= $request->input();
     $ok=AdminModel::where('id',$id)->first();
//     dd($ok);
     $ok->delete($ok);
        if($ok){
            return "数据删除成功<br>
            <a href='form'>数据添加</a><br>
            <a href='table'>查看数据</a>
            ";
        }else{
            return "数据删除失败<br>
            <a href='form'>数据添加</a><br>
            <a href='table'>查看数据</a>";
        }


}

}
